<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jul 30, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

class TagsControl {
	var $_tagDto;
	
	function __construct() {
		RExtImporter::cls( 'tretoday.dbo.TagCategoryView' );
	}
	
	function getWeightedTags($catCode = null, $step = 8, $numberOfTags = 50) {
		
		if (! empty ( $catCode )) {
			$tagDto = new TagCategoryView ( 'WHERE t.content_count > 0 AND c.cat_code = ' . "'$catCode'", 'ORDER BY t.content_count DESC LIMIT 0,' . $numberOfTags );
		} else {
			$tagDto = new TagCategoryView ( 'WHERE t.content_count > 0', 'ORDER BY t.content_count DESC LIMIT 0,' . $numberOfTags );
		}
		
		SysDatabase::setQuery ( $tagDto->getSelectSQL () );
		$result = SysDatabase::loadObjectList ();
		
		return $this->buildWeightedTags ( $result, $step );
	}
	
	function buildWeightedTags($result, $steps = 8) {
		$tags = array ();
		$min = 1e9;
		$max = - 1e9;
		
		foreach ( $result as $tag ) {
			$tag->content_count = log ( $tag->content_count );
			$min = min ( $min, $tag->content_count );
			$max = max ( $max, $tag->content_count );
			$tags [$tag->tag_id] = $tag;
		}
		$range = max ( .01, $max - $min ) * 1.0001;
		
		foreach ( $tags as $key => $value ) {
			$tags [$key]->level = 1 + floor ( $steps * ($value->content_count - $min) / $range );
		}
		return $tags;
	}
	
	function themeWeightedTags($tags) {
		RExtImporter::cls( 'tretoday.dbo.CategoryDTO' );
		$res = new SiteResource ( 'news' );
		
		$link = $res->getLink ();
		$arrTags = array ();
		shuffle ( $tags );
		foreach ( $tags as $tag ) {
			if (! empty ( $tag->cat_id )) {
				$catDto = new CategoryDTO ( 'WHERE cat_id = ' . $tag->cat_id );
				SysDatabase::setQuery ( $catDto->getSelectSQL () );
				
				$result = SysDatabase::loadObject ();
				$res->action = $result->cat_code;
				$res->task = $tag->tag_id;
				$link = $res->getLink ();
				$name = $tag->tag_name;
				$style = '';
			} else {
				$style = 'target="_blank"';
				$link = $tag->url;
				$name = $tag->ads_tag_name;
			}
			$arrTags [] = "<a href=\"$link\" $style class=\"link{$tag->level}\">" . $name . "</a>";
		}
		
		$output = implode ( ', ', $arrTags );
		return $output;
	}
}

?>